<template>
  <div class="pdf-preview">
    <div class="pdf-container" v-loading="loading">
      <iframe 
        v-if="fileInfo.fileUrl" 
         :src="`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(fileInfo.fileUrl)}`"
        @load="handlePdfLoad"
        @error="handlePdfError"
        class="preview-pdf"
        frameborder="0"
      ></iframe>
      
      <div v-else-if="error" class="error-message">
        <el-icon><Document /></el-icon>
        <p>PDF文件加载失败</p>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Document, Download, Link } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  fileInfo: {
    type: Object,
    default: () => ({})
  }
})

const loading = ref(true)
const error = ref(false)

// PDF加载完成
const handlePdfLoad = () => {
  loading.value = false
  error.value = false
}

// PDF加载错误
const handlePdfError = () => {
  error.value = true
  loading.value = false
}
  

onMounted(() => {
  
  // 初始化时设置加载状态
  if (props.fileInfo.fileUrl) {
    loading.value = true
    error.value = false
  }
})
</script>

<style scoped>
.pdf-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.pdf-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 8px;
  min-height: 500px;
  position: relative;
}

.preview-pdf {
  width: 100%;
  height: 100%;
  border-radius: 4px;
}

.error-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 14px;
  gap: 16px;
}

.error-message .el-icon {
  font-size: 48px;
}

</style>
